local map = require('utils.vim').map
local helper = require('helper.telescope')
local helper_config = require('helper.telescope_config')
local helper_fb_config = require('helper.telescope_fb_config')
local helper_picker_config = require('helper.telescope_picker_config')
local helper_undo_config = require('helper.telescope_undo_config')

return {
    "nvim-telescope/telescope.nvim",
    lazy = false,
    dependencies = {
        {"nvim-lua/plenary.nvim"},
        {"debugloop/telescope-undo.nvim"},
        {"nvim-telescope/telescope-file-browser.nvim"},
        {"nvim-telescope/telescope-fzf-native.nvim", build = "make"},
    },
    config = function()
        local telescope = require("telescope")
        local actions = require("telescope.actions")
        local fb_actions = require "telescope".extensions.file_browser.actions
        local undo_actions = require("telescope-undo.actions")

        telescope.setup({
            pickers = helper_picker_config.get_default_mappings(actions),
            extensions = {
                fzf = {
                    fuzzy = true,                    -- false will only do exact matching
                    override_generic_sorter = true,  -- override the generic sorter
                    override_file_sorter = true,     -- override the file sorter
                    case_mode = "smart_case",        -- or "ignore_case" or "respect_case"
                },
                file_browser = {
                    --initial_mode = '',
                    dir_icon_hl = 'TelescopePreviewDirectory',
                    prompt_path = true,
                    display_stat = false,
                    hijack_netrw = true,
                    mappings = helper_fb_config.get_default_mappings(helper,actions,fb_actions),
                }, -- file_browser
                undo = {
                    initial_mode = 'normal',
                    mappings = helper_undo_config.get_default_mappings(undo_actions),
                }, -- file_browser
            },
            defaults = {
                path_display = { "smart" }, -- smart,truncate,shorten
                mappings = helper_config.get_default_mappings(helper,actions),
                sorting_strategy = "ascending",
                layout_strategy = "bottom_pane",
                results_title = "",
                file_ignore_patterns = { "node_modules",".pyc" },
                prompt_prefix=" ",
                selection_caret = "󱞩 ",
                preview=false,
                layout_config = {
                    bottom_pane = {
                        height = 0.99,
                        prompt_position = "top",
                    }
                },
            },
        })
        -- load extensions
        telescope.load_extension("undo")
        telescope.load_extension("fzf")
        telescope.load_extension("file_browser")

        -- key map
        map("n","<M-r>", "<cmd>Telescope resume<cr>",{desc="resume last search"})
        map("n","<M-u>", "<cmd>lua require('telescope').extensions.undo.undo({initial_mode = 'normal'})<cr>",{desc="show undo history"})
        map("n","<space>c", helper.pick_cmd_history,{desc="show command history"})
        map("n","<leader>k", helper.pick_key_maps,{desc="show key maps"})

        -- map("n","<M-h>gs", function()
        --     helper.pick_snippets(false)
        -- end,{desc="show snippets (live grep)"})
        --map("n","<M-h>fs", function()
        map("n","<M-i>", function()
            helper.pick_snippets(true)
        end,{desc="show snippets (find files)"})

        -- map("n","<M-h>gp", function()
        --     helper.pick_info(false)
        -- end,{desc="show knowledge(js/py, live grep)"})
        -- map("n","<M-h>fp", function()
        --     helper.pick_file_browser('~/devhelper/myinfo')
        -- end,{desc="show knowledge(js/py, file browser)"})

        -- map("n","<M-h>gw", function()
        --     helper.pick_info_wx(false)
        -- end,{desc="show knowledge(wx, live grep)"})
        -- map("n","<M-h>fw", function()
        --     helper.pick_file_browser('~/devhelper/myinfowx')
        -- end,{desc="show knowledge(wx, file browser)"})

        map("n","<space>f", helper.pick_file_browser,{desc="show file browser"})

        map("n",",z",function()
            helper.pick_fuzzy_find(false)
        end,{desc="show fuzzy find"})
        map("x",",z",function()
            helper.pick_fuzzy_find(true)
        end,{desc="show fuzzy find for selected text"})

        map("n","<M-g>",function()
            helper.pick_live_grep(false)
        end,{desc="live grep"})

        --map({"n","x"},"<M-f>",helper.pick_find_files,{desc="find files"})

        --map("n","<leader>f",function()
        map("n","<M-f>",function()
            local cwd = vim.fn.getcwd()
            require("fff").find_files_in_dir(cwd)
        end,{desc="use fff to find files"})

    end,
}
-- EOF
